package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.User;
import mil.af.amc.adpetracker.model.comparators.EquipmentItemManufacturerComparator;
import mil.af.amc.adpetracker.model.comparators.EquipmentItemSerialComparator;
import mil.af.amc.adpetracker.model.comparators.UserDisplaynameComparator;
import mil.af.amc.adpetracker.service.OrganizationService;
import mil.af.amc.adpetracker.service.UserService;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * View Helper for User objects
 *
 * @author TSgt Jason Ferguson
 */
@Service("userViewHelper")
public class UserViewHelper implements PageContextAwareBean {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private OrganizationService organizationService;

    @Autowired
    public void setOrganizationService(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    private int userId;

    public void setUserId(int userId) {
        this.userId = userId;
    }

    private PageContext pageContext;

    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    public User getUser() {

        int userId = Integer.parseInt(pageContext.getRequest().getParameter("userId"));
        return userService.getUserById(userId);
    }

    public List<User> getAll() {

        List<User> userList = userService.getAll();
        Collections.sort(userList, new UserDisplaynameComparator());
        return userList;
    }

    public List<User> getAllByOrganization() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        int organizationId = 0;
        if (request.getParameter("organizationId") != null) {
            organizationId = Integer.parseInt(request.getParameter("organizationId"));
        }
        if (request.getAttribute("organizationId") != null) {
            organizationId = (Integer) request.getAttribute("organizationId");
        }

        // If the organizationId doesn't exist, I just want an empty list
        if (organizationId == 0) {
            return new ArrayList<User>();
        }

        List<User> results = userService.getAllByOrganization(organizationId);
        Collections.sort(results);

        return results;
    }

    public List<User> getAllByOrganization2() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        Integer organizationId = (Integer) request.getSession().getAttribute("organizationId");
        if (organizationId == null || organizationId == 0) {
            return null;
        }

        List<User> results = userService.getAllByOrganization(organizationId);
        Collections.sort(results);
        return results;
    }

/*    public List<AdpeAccount> getAccountAdpeAccounts() {

        int id = (Integer) pageContext.getRequest().getAttribute("userId");
        //User user = pageContext.getRequest().getAttribute("userId");
        User user = userService.getUserById(id);
        return user.getAccountAdpeAccounts();

    }*/

    public List<EquipmentItem> getAssignedEquipment() {

        int userId = Integer.parseInt(pageContext.getRequest().getParameter("userId"));
        User user = userService.getUserById(userId);
        List<EquipmentItem> itemList = user.getAssignedEquipment();
        Collections.sort(itemList, new EquipmentItemSerialComparator());
        Collections.sort(itemList, new EquipmentItemManufacturerComparator());
        return itemList;
    }

//    @SuppressWarnings({"ForLoopReplaceableByForEach"})
//    public List<User> getUsersWithNoCpu() {
//
//        int userId = (Integer) pageContext.getRequest().getAttribute("userId");
//        List<User> userList = userService.getAll();
//        List<User> returnList = new ArrayList<User>();
//
//        for (Iterator<User> it = userList.iterator(); it.hasNext();) {
//
//            User user = it.next();
//            List<EquipmentItem> itemList = user.getAssignedEquipment();
//            boolean flag = false;
//            for (Iterator<EquipmentItem> it2 = itemList.iterator(); it2.hasNext();) {
//
//                EquipmentItem item = it2.next();
//                if (item.getItemType().getName().equals("WORKSTATION")) {
//                    flag = true;
//                    break;
//                }
//
//                // If the User had an item that was a workstation, the break should have
//                // caught it
//                returnList.add(user);
//
//            }
//        }
//
//        return returnList;
//
//    }

    public List<User> getUsersWithNoAssignedEquipment() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        Integer organizationId = (Integer) request.getAttribute("organizationId");

        if (organizationId == null) {
            return new ArrayList<User>();
        }

        List<User> results = userService.getUsersWithNoAssignedEquipment(organizationId);
        Collections.sort(results);

        return results;

    }

    public List<User> getUsersWithNoAssignedCpu() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        Integer organizationId = (Integer) request.getAttribute("organizationId");

        if (organizationId == null) {
            return new ArrayList<User>();
        }

        List<User> results = userService.getUsersWithNoAssignedCpu(organizationId);
        Collections.sort(results);

        return results;
    }
}
